home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MISC.SWG / 0061_Pick Unit; Select Choice.pas < prev    next >
Pascal/Delphi Source File  |  1993-11-26  |  13KB  |  519 lines

  1. (********************************************************)
  2. (******************** PICK.PAS **************************)
  3. (******* the pick unit; to select menu choice *******)
  4.  
  5. Unit Pick;
  6.  
  7. interface
  8.  
  9. {1} Function ScreenChar : Char; {return the char at the cursor}
  10. {2} Procedure BlockCursor; {give us a block cursor; TP6 & 7 only}
  11. {3} Procedure NormalCursor; {restore cursor to normal; TP6 & 7 only}
  12.  
  13. {4} Function PickByte(Left, Top, Bottom : Byte) : Byte;
  14.     {return the number of the item chosen as a byte, or
  15.     return ZERO if ESCape is pressed}
  16.  
  17. {5} Function PickChar(Left, Top, Bottom : Byte) : Char;
  18.     {return the character at the cursor when ENTER is pressed}
  19.  
  20.  
  21. {
  22. Notes: for "Pick" functions
  23.   One returns a Byte and the other returns a Char - use one
  24.   or the other;
  25.  
  26.   Parameters:
  27.   Left   = the left side of the menu list (left side of window+1)
  28.   Top    = the top of the menu list       (top of window+1)
  29.   Bottom = the bottom of the menu list;   (bottom of window-1)
  30. }
  31.  
  32. implementation
  33.  
  34. uses
  35. dos,
  36. crt,
  37. keyb;
  38.  
  39. {-----------------------------------------------------}
  40. Function PickByte(Left,Top,Bottom : byte) : Byte;
  41. {return the number of the item chosen as a byte, or
  42. return ZERO if ESCape is pressed}
  43.  
  44. Var
  45. x,y,x1,y1 : byte;
  46. ch        : char;
  47. int,total : byte;
  48.  
  49. begin
  50.     PickByte := 0;              {default to ZERO}
  51.     total := (Bottom - Top)+1;  {total number of items in list}
  52.     x1 := WhereX; y1 := WhereY; {save the original location}
  53.  
  54.     x := Left; y := Top;
  55.     BlockCursor;         {give us a block cursor}
  56.  
  57.     GotoXy(x, y);
  58.  
  59.     int := 1;
  60.  
  61.     Repeat
  62.        Ch := GetKey;
  63.  
  64.        Case Ch of
  65.           LeftArrow, UpArrow : {move up}
  66.           begin
  67.              If y = Top then
  68.              begin
  69.                y := Bottom;
  70.                int := total;
  71.              end
  72.              else
  73.              begin
  74.                Dec(y);
  75.                dec(int);
  76.              end;
  77.  
  78.            GotoXy(x,y);
  79.           end; {leftarrow}
  80.  
  81.           RightArrow, DownArrow :   {move down}
  82.           begin
  83.              If y = Bottom then
  84.              begin
  85.                 y := Top;
  86.                 int := 1;
  87.              end
  88.              else
  89.              begin
  90.                 Inc(y);
  91.                 inc(int);
  92.              end;
  93.              GotoXy(x,y);
  94.           end; {rightarrow}
  95.  
  96.         PgUp, Home : {go to top of list}
  97.         begin
  98.             y := Top;
  99.             int := 1;
  100.             GotoXy(x,y);
  101.         end;
  102.  
  103.         PgDn, EndKey :  {go to bottom of list}
  104.         begin
  105.             y := Bottom;
  106.             int := total;
  107.             GotoXy(x,y);
  108.         end;
  109.  
  110.        #13 : PickByte := int; {return position of choice in the array}
  111.      End; {Case Ch}
  112.  
  113.     Until (ch = #27) or (ch = #13); {loop until ESCape or ENTER}
  114.  
  115.     GotoXY(x1,y1);  {return to original location}
  116.     NormalCursor;   {Restore the cursor}
  117. end;
  118. {---------------------------------------------}
  119.  
  120. Function PickChar(Left, Top,Bottom : byte) : Char;
  121. {return the character at the cursor when ENTER is pressed}
  122.  
  123. Var
  124. x,y,x1,y1 : byte;
  125. ch    : char;
  126.  
  127. begin
  128.     PickChar := #27;
  129.     x1 := WhereX; y1 := WhereY;
  130.     x := Left; y := Top;
  131.  
  132.     BlockCursor;         {give us a block cursor}
  133.     GotoXy(x,y);
  134.  
  135.     Repeat
  136.        Ch := GetKey;
  137.        Case Ch of
  138.           LeftArrow, UpArrow :
  139.           begin
  140.              If y = Top then y := Bottom else Dec(y);
  141.              GotoXy(x,y);
  142.           end; {leftarrow}
  143.  
  144.           RightArrow, DownArrow :
  145.           begin
  146.              If y = Bottom then y := Top else Inc(y);
  147.              GotoXy(x,y);
  148.           end; {leftarrow}
  149.  
  150.         PgUp, Home :
  151.         begin
  152.             y := Top;
  153.             GotoXy(x,y);
  154.         end;
  155.  
  156.         PgDn, EndKey :
  157.         begin
  158.             y := Bottom;
  159.             GotoXy(x,y);
  160.         end;
  161.  
  162.        #13 : PickChar := ScreenChar; {return the char under the cursor}
  163.      End; {Case Ch}
  164.  
  165.     Until (ch = #27) or (ch = #13);
  166.     GotoXY(x1,y1);
  167.     NormalCursor;         {give us a block cursor}
  168.  
  169. end;
  170. {-----------------------------------------------}
  171.  
  172. {----------------------------------------}
  173. Function ScreenChar : Char; {return the character at the cursor}
  174. Var
  175. R : Registers;
  176. begin
  177.    Fillchar(R, SizeOf(R), 0);
  178.    R.AH := 8;
  179.    R.BH := 0;
  180.    Intr($10, R);
  181.    ScreenChar := Chr(R.AL);
  182. end;
  183. {--------------------------------------------------}
  184.  
  185.  
  186. {---------------------------------}
  187. Procedure NormalCursor; {restore cursor to normal; TP6 & 7 only}
  188. BEGIN
  189.  asm
  190.   mov ah,1
  191.   mov ch,5   { / You will want to fool around with these two}
  192.   mov cl,6   { \ numbers to get the cursor you want}
  193.   int $10
  194.  END;
  195. END;
  196.  
  197. {--------------------------------}
  198. Procedure BlockCursor; {give us a block cursor; TP6 & 7 only}
  199. BEGIN
  200.  asm
  201.   mov ah,1
  202.   mov ch,5    { / You will want to fool around with these two}
  203.   mov cl,8    { \ numbers to get the cursor you want; (1=big)}
  204.   int $10
  205.  END;
  206. END;
  207. {-------------------------------------}
  208.  
  209. End.
  210.  
  211. {----------------- end of PICK.PAS --------------------}
  212.  
  213.  
  214.  
  215.  
  216. (********************************************************)
  217. (******************** KEYB.PAS **************************)
  218. (******* the keyboard unit; for GetKey() function *******)
  219.  
  220. Unit Keyb;
  221.  
  222. Interface
  223.  
  224. Uses Crt;
  225.  
  226. Const
  227.         F1  = #187;
  228.         F2  = #188;
  229.         F3  = #189;
  230.         F4  = #190;
  231.         F5  = #191;
  232.         F6  = #192;
  233.         F7  = #193;
  234.         F8  = #194;
  235.         F9  = #195;
  236.         F10 = #196;
  237.  
  238.         ALTF1  = #232;
  239.         ALTF2  = #233;
  240.         ALTF3  = #234;
  241.         ALTF4  = #235;
  242.         ALTF5  = #236;
  243.         ALTF6  = #237;
  244.         ALTF7  = #238;
  245.         ALTF8  = #239;
  246.         ALTF9  = #240;
  247.         ALTF10 = #241;
  248.  
  249.         CTRLF1        = #222;
  250.         CTRLF2        = #223;
  251.         CTRLF3        = #224;
  252.         CTRLF4        = #225;
  253.         CTRLF5        = #226;
  254.         CTRLF6        = #227;
  255.         CTRLF7        = #228;
  256.         CTRLF8        = #229;
  257.         CTRLF9        = #230;
  258.         CTRLF10 = #231;
  259.  
  260.         SHFTF1        = #212;
  261.         SHFTF2        = #213;
  262.         SHFTF3        = #214;
  263.         SHFTF4        = #215;
  264.         SHFTF5        = #216;
  265.         SHFTF6        = #217;
  266.         SHFTF7        = #218;
  267.         SHFTF8        = #219;
  268.         SHFTF9        = #220;
  269.         SHFTF10 = #221;
  270.  
  271.         UPARROW    = #200;
  272.         RIGHTARROW = #205;
  273.         LEFTARROW  = #203;
  274.         DOWNARROW  = #208;
  275.  
  276.         HOME           = #199;
  277.         PGUP           = #201;
  278.         ENDKEY           = #207;
  279.         PGDN           = #209;
  280.         INS                      = #210;
  281.         DEL                      = #211;
  282.         TAB                      = #9;
  283.         ESC                      = #27;
  284.         ENTER           = #13;
  285.         SYSREQ           = #183;
  286.         CTRLMINUS  = #31;
  287.         SPACE           = #32;
  288.         CTRL2           = #129;
  289.         CTRL6           = #30;
  290.         BACKSPACE  = #8;
  291.         BS                      = #8; {2 NAMES FOR BACKSPACE}
  292.  
  293.         CTRLBACKSLASH         = #28;
  294.         CTRLLEFTBRACKET  = #27;
  295.         CTRLRIGHTBRACKET = #29;
  296.         CTRLBACKSPACE         = #127;
  297.         CTRLBS                          = #127;
  298.  
  299.         ALTA = #158;
  300.         ALTB = #176;
  301.         ALTC = #174;
  302.         ALTD = #160;
  303.         ALTE = #146;
  304.         ALTF = #161;
  305.         ALTG = #162;
  306.         ALTH = #163;
  307.         ALTI = #151;
  308.         ALTJ = #164;
  309.         ALTK = #165;
  310.         ALTL = #166;
  311.         ALTM = #178;
  312.         ALTN = #177;
  313.         ALTO = #152;
  314.         ALTP = #153;
  315.         ALTQ = #144;
  316.         ALTR = #147;
  317.         ALTS = #159;
  318.         ALTT = #148;
  319.         ALTU = #150;
  320.         ALTV = #175;
  321.         ALTW = #145;
  322.         ALTX = #173;
  323.         ALTY = #149;
  324.         ALTZ = #172;
  325.  
  326.         CTRLA = #1;
  327.         CTRLB = #2;
  328.         CTRLC = #3;
  329.         CTRLD = #4;
  330.         CTRLE = #5;
  331.         CTRLF = #6;
  332.         CTRLG = #7;
  333.         CTRLH = #8;
  334.         CTRLI = #9;
  335.         CTRLJ = #10;
  336.         CTRLK = #11;
  337.         CTRLL = #12;
  338.         CTRLM = #13;
  339.         CTRLN = #14;
  340.         CTRLO = #15;
  341.         CTRLP = #16;
  342.         CTRLQ = #17;
  343.         CTRLR = #18;
  344.         CTRLS = #19;
  345.         CTRLT = #20;
  346.         CTRLU = #21;
  347.         CTRLV = #22;
  348.         CTRLW = #23;
  349.         CTRLX = #24;
  350.         CTRLY = #25;
  351.         CTRLZ = #26;
  352.  
  353.         ALT1 = #248;
  354.         ALT2 = #249;
  355.         ALT3 = #250;
  356.         ALT4 = #251;
  357.         ALT5 = #252;
  358.         ALT6 = #253;
  359.         ALT7 = #254;
  360.         ALT8 = #255;
  361.         ALT9 = #167;
  362.         ALT0 = #168;
  363.  
  364.         ALTMINUS = #169;
  365.         ALTEQ         = #170;
  366.         SHIFTTAB = #143;
  367.  
  368. Function GetKey : Char;
  369. procedure unGetKey(C : char);
  370. procedure FlushKbd;
  371. procedure flushBuffer;
  372.  
  373. const
  374.     hasPushedChar   : boolean = false;
  375.  
  376. implementation
  377. var
  378.     pushedChar            : char;
  379.  
  380.  
  381. (******************************************************************************
  382. *                                  FlushKbd                                  *
  383. ******************************************************************************)
  384. procedure FlushKbd;
  385. var
  386.     C        : char;
  387. begin
  388.     hasPushedChar := False;
  389.     while (KeyPressed) do
  390.          C := GetKey;
  391. end; {flushKbd}
  392.  
  393. (******************************************************************************
  394. *                                 flushBuffer                                 *
  395. * Same as above, but if key was pushed by eventMgr, know about it !!          *
  396. ******************************************************************************)
  397. procedure flushBuffer;
  398. var
  399.    b : boolean;
  400. begin
  401.    b := hasPushedChar;
  402.    flushKbd;
  403.    hasPushedChar := b;
  404. end; {flushBuffer}
  405.  
  406.  
  407. (******************************************************************************
  408. *                                  unGetKey                                   *
  409. * UnGetKey will put one character back in the input buffer. Push-back buffer  *
  410. * can contain only one character.                                                                  *
  411. * To avoid problems DO NOT CALL UNGETKEY WITHOUT FIRST CALLING GETKEY. If two *
  412. * characters are pushed, the first is discarded.                                          *
  413. ******************************************************************************)
  414. procedure unGetKey;
  415. begin
  416.     hasPushedChar := True;
  417.     pushedChar          := c;
  418. end; {unGetKey}
  419.  
  420. (******************************************************************************
  421. *                                   GetKey                                   *
  422. ******************************************************************************)
  423. function GetKey : Char;
  424. var
  425.         c : Char;
  426. Begin
  427.     if (hasPushedChar) then begin
  428.                 GetKey              := pushedChar;
  429.                 hasPushedChar := False;
  430.                 exit;
  431.     end;
  432.     c := ReadKey;
  433.     if (Ord(c) = 0) then Begin
  434.                 c := ReadKey;
  435.                 if c in [#128,#129,#130,#131]
  436.                     then c := chr(ord(c) + 39)
  437.                 else c := chr(ord(c) + 128); {map to suit keyboard constants}
  438.     End;
  439.     GetKey := c; {return keyboard (my..) code }
  440. End; {getKey}
  441.  
  442. End.
  443. {--------------- End of KEYB.PAS ---------------}
  444.  
  445.  
  446. (********************************************************)
  447. (************************** TEST.PAS ********************)
  448. (*************** to test the PICK unit ******************)
  449. (*************** quit by pressing ESCape ****************)
  450.  
  451. Program Test;
  452.  
  453. uses crt,pick;
  454.  
  455. {--------------- test program -----------------}
  456. const
  457. max = 6;
  458. s : array[1..max] of string[18] =
  459. (
  460. '1. Number One ',
  461. '2. Number Two ',
  462. '3. Number Three ',
  463. '4. Number Four ',
  464. '5. Number Five ',
  465. '6. Number Six ');
  466.  
  467. var
  468. i  : byte;
  469. x  : byte;
  470. ch : char;
  471. j  : byte;
  472.  
  473. begin
  474.     clrscr;
  475.     x := 10; {left side of the list}
  476.  
  477.  
  478.    {------------------------- test using PickByte() ----------------}
  479.     for i := 1 to max do
  480.     begin            {display the list of menu items}
  481.       j := i+5;      {start from row 6}
  482.       gotoxy(x,j);
  483.       writeln(s[i]);
  484.     end;
  485.  
  486.     i := j;
  487.     repeat
  488.       {ch := choice(x,1,i);}
  489.       j := pickbyte(x,6,i);
  490.  
  491.       gotoxy(15,22);
  492.       writeln('You chose ',j);
  493.     until j = 0; {until Escape}
  494.  
  495.    {------------------------- test using PickChar() ----------------}
  496.     ClrScr;
  497.  
  498.     ch := 'A';
  499.     for i := 1 to max do
  500.     begin
  501.        s[i][1] := Ch; {change numbers to letters in menu list}
  502.        Inc(Ch);
  503.     end;
  504.  
  505.     for i := 1 to max do
  506.     begin            {display the list of menu items}
  507.       gotoxy(x,i);   {start from row 1}
  508.       writeln(s[i]);
  509.     end;
  510.  
  511.     repeat
  512.       ch := PickChar(x,1,i);
  513.       gotoxy(15,22);
  514.       writeln('You chose ',ch);
  515.     until ch = #27;  {until Escape}
  516.  
  517. end.
  518. {------------------------ end of TEST.PAS ---------------------------}
  519.